package blackstar.agent;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import blackstar.agent.exception.ConfigException;
import blackstar.agent.util.PropertyConfig;

/**
 * @author RayWoo (ayufox@gmail.com)
 * 
 */
public class AgentConfig
{
	public final static String AGENT_HOME = "AGENT_HOME";
	private final static Log LOGGER = LogFactory.getLog(AgentConfig.class);
	private static String agentHome;
	private static String configPath;
	private static String dataPath;
	private static String logPath;

	private static PropertyConfig propertyConfig = new PropertyConfig();

	static
	{
		agentHome = System.getenv(AGENT_HOME);
		if (isEmpty(agentHome))
		{
			agentHome = System.getProperty(AGENT_HOME);
		}

		if (isEmpty(agentHome))
		{
			try
			{
				agentHome = (new File(System.getProperty("user.dir"), ".."))
						.getCanonicalPath();
			} catch (IOException e)
			{
				LOGGER.error("Error Get Agent Home", e);
			}
		}

		if (isEmpty(agentHome))
		{
			throw new ConfigException("AGENT_HOME not found");
		}
		
		propertyConfig.addProperty(AGENT_HOME, agentHome);

		configPath = agentHome + File.separator + "config";
		dataPath = agentHome + File.separator + "data";
		logPath = agentHome + File.separator + "log";

		File config = new File(configPath + File.separator
				+ "config.properties");
		if (config.exists())
		{
			try
			{
				propertyConfig.load(new FileInputStream(config));
			} catch (Exception e)
			{
				throw new ConfigException("Error Load Config["
						+ config.getAbsolutePath() + "]", e);
			}
		}

		config = new File(configPath + File.separator + "config.xml");
		if (config.exists())
		{
			try
			{
				propertyConfig.loadFromXML(new FileInputStream(config));
			} catch (Exception e)
			{
				throw new ConfigException("Error Load Config["
						+ config.getAbsolutePath() + "]", e);
			}
		}
	}

	public static String getWebIP()
	{
		return AgentConfig.getProperty("agent.ip");
	}

	public static int getWebPort()
	{
		String port = AgentConfig.getProperty("agent.port");
		if (isEmpty(port))
		{
			return 8080;
		} else
		{
			return Integer.parseInt(port.trim());
		}
	}

	private static boolean isEmpty(String str)
	{
		return (str == null || str.trim().length() == 0);
	}

	public static String getAgentHome()
	{
		return agentHome;
	}

	public static String getConfigPath()
	{
		return configPath;
	}

	public static String getDataPath()
	{
		return dataPath;
	}

	public static String getLogPath()
	{
		return logPath;
	}

	public static String getProperty(String name)
	{
		return propertyConfig.getVariableValue(name);
	}
	
	public static Properties getProperties()
	{
		return propertyConfig.getProperties();
	}

	public static String getMonitorDataPath(String jvmid)
	{
		return getDataPath() + File.separator + jvmid + File.separator
				+ "monitor";
	}

	public static String getPerformanceLogPath(String jvmid)
	{
		return getLogPath() + File.separator + jvmid + File.separator
				+ "performance";
	}

	public static String getPerformanceDataPath(String jvmid)
	{
		return getDataPath() + File.separator + jvmid + File.separator
				+ "performance";
	}

	public static String getWebPath()
	{
		return getAgentHome() + File.separator + "webapps";
	}

	public static String getWebTempPath()
	{
		return getAgentHome() + File.separator + "webtemps";
	}

	public static String getAgentLib(String agentType)
	{
		String libpath = getProperty("agentlib." + agentType);
		if (libpath == null || libpath.length() == 0)
		{
			return null;
		}

		return libpath;
	}
}
